package com.hnucm.c202001020214.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hnucm.c202001020214.BookAdapter;
import com.hnucm.c202001020214.Book_qw;
import com.hnucm.c202001020214.Electronics;
import com.hnucm.c202001020214.Others;
import com.hnucm.c202001020214.R;
import com.hnucm.c202001020214.RetrofitUtils;
import com.hnucm.c202001020214.Search_results;
import com.hnucm.c202001020214.api.Api;
import com.hnucm.c202001020214.object.Book;
import com.hnucm.c202001020214.object.Goods;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class Warehouse extends Fragment {
    public List<Book> bookList = new ArrayList<>();
    public List<Goods.ItemsDTO> items=new ArrayList<>();
    RecyclerView recyclerView;
    ImageView imageView5,imageView6,imageView8;
    TextView textView19,textView25;
    String gi,gn,gp;
    String rt;
//    Goods goods=new Goods();
    EditText editText;

//    public List<Goods.ItemDTO> rty =new ArrayList<>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_warehouse, container, false);
        textView19 = view.findViewById(R.id.textView19);

        editText = view.findViewById(R.id.editText);

        textView25 = view.findViewById(R.id.textView25);
        textView25.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                rt = editText.getText().toString();
//        Log.i("qwe",it);
                Api api1 = RetrofitUtils.getRetrofit("https://api-gw.onebound.cn/jd/").create(Api.class);
                Call<Goods> goodsCall = api1.getAsd("t7608413987","20220619",rt);
                goodsCall.enqueue(new Callback<Goods>() {
                    @Override
                    public void onResponse(Call<Goods> call, Response<Goods> response) {

                        Goods goods = response.body();
                        Log.i("qwe",goods.items.item.get(0).getPrice());
//                        List<Goods.ItemsDTO.ItemDTO> a=goods.items.item;
                        EventBus.getDefault().postSticky(goods.items.item);
//                        gi = goods.items.item.add(goods.getItems().getItem().get());

                    }

                    @Override
                    public void onFailure(Call<Goods> call, Throwable t) {

                    }
                });
                Intent intent = new Intent(getActivity(), Search_results.class);
                startActivity(intent);

            }
        });



        initBook();

        recyclerView =view.findViewById(R.id.recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);

        BookAdapter adapter = new BookAdapter(bookList);
        recyclerView.setAdapter(adapter);

        setOnclick(view);

        return view;


    }



    public void setOnclick (View view){

        imageView5 = view.findViewById(R.id.imageView5);
        imageView5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Book_qw.class);
                startActivity(intent);
            }
        });

        imageView6 = view.findViewById(R.id.imageView6);
        imageView6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Electronics.class);
                startActivity(intent);
            }
        });

        imageView8 = view.findViewById(R.id.imageView8);
        imageView8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Others.class);
                startActivity(intent);
            }
        });

    }

    //初始化
    public void initBook(){
        Book book01 = new Book(R.drawable.book_01,"程序员的英语","48.88元");
        bookList.add(book01);
        Book book02 = new Book(R.drawable.e_02,"华为平板","999.99元");
        bookList.add(book02);
        Book book03 = new Book(R.drawable.e_03,"二手鼠标","28.00元");
        bookList.add(book03);
        Book book04 = new Book(R.drawable.book_04,"IT文化","56.00元");
        bookList.add(book04);
        Book book05 = new Book(R.drawable.qita01,"台灯","19.00元");
        bookList.add(book05);
        Book book06 = new Book(R.drawable.book_06,"数据结构和算法实战","58.88元");
        bookList.add(book06);
        Book book08 = new Book(R.drawable.qita06,"计算器","9.00元");
        bookList.add(book08);
        Book book09 = new Book(R.drawable.book_09,"IT运维之道","56.00元");
        bookList.add(book09);

    }

}


